<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Create index API | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'indices-create-index.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/indices-create-index.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/indices-create-index.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/indices-create-index.html" rel="nofollow" target="_blank">../en/indices-create-index.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="rest-apis.html">REST APIs</a></span>
»
<span class="breadcrumb-link"><a href="indices.html">Index APIs</a></span>
»
<span class="breadcrumb-node">Create index API</span>
</div>
<div class="navheader">
<span class="prev">
<a href="indices-close.html">« Close index API</a>
</span>
<span class="next">
<a href="indices-delete-index.html">Delete index API »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="indices-create-index"></a>Create index API<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h2>
</div></div></div>

<p>Creates a new index.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /twitter</pre>
</div>
<div class="console_widget" data-snippet="snippets/1620.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="indices-create-api-request"></a>Request<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h3>
</div></div></div>
<p><code class="literal">PUT /&lt;index&gt;</code></p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="indices-create-api-desc"></a>Description<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h3>
</div></div></div>
<p>You can use the create index API to add a new index to an Elasticsearch cluster. When
creating an index, you can specify the following:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
Settings for the index
</li>
<li class="listitem">
Mappings for fields in the index
</li>
<li class="listitem">
Index aliases
</li>
</ul>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="indices-create-api-path-params"></a>Path parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">&lt;index&gt;</code>
</span>
</dt>
<dd>
<p>(Required, string) Name of the index you wish to create.</p>
<p>Index names must meet the following criteria:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
Lowercase only
</li>
<li class="listitem">
Cannot include <code class="literal">\</code>, <code class="literal">/</code>, <code class="literal">*</code>, <code class="literal">?</code>, <code class="literal">"</code>, <code class="literal">&lt;</code>, <code class="literal">&gt;</code>, <code class="literal">|</code>, ` ` (space character), <code class="literal">,</code>, <code class="literal">#</code>
</li>
<li class="listitem">
Indices prior to 7.0 could contain a colon (<code class="literal">:</code>), but that’s been deprecated and won’t be supported in 7.0+
</li>
<li class="listitem">
Cannot start with <code class="literal">-</code>, <code class="literal">_</code>, <code class="literal">+</code>
</li>
<li class="listitem">
Cannot be <code class="literal">.</code> or <code class="literal">..</code>
</li>
<li class="listitem">
Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster)
</li>
<li class="listitem">
Names starting with <code class="literal">.</code> are deprecated, except for <a class="xref" href="index-modules.html#index-hidden">hidden indices</a> and internal indices managed by plugins
</li>
</ul>
</div>
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="indices-create-api-query-params"></a>Query parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">include_type_name</code>
</span>
</dt>
<dd>
<span class="Admonishment Admonishment--change">
[<span class="Admonishment-version u-mono u-strikethrough">7.0.0</span>]
<span class="Admonishment-detail">
Deprecated in 7.0.0. Mapping types have been deprecated. See <a class="xref" href="removal-of-types.html" title="Removal of mapping types"><em>Removal of mapping types</em></a>.
</span>
</span>
(Optional, boolean) If <code class="literal">true</code>, a mapping type is expected in the body of
mappings. Defaults to <code class="literal">false</code>.
</dd>
<dt>
<span class="term">
<code class="literal">wait_for_active_shards</code>
</span>
</dt>
<dd>
<p>(Optional, string) The number of shard copies that must be active before
proceeding with the operation. Set to <code class="literal">all</code> or any positive integer up
to the total number of shards in the index (<code class="literal">number_of_replicas+1</code>).
Default: 1, the primary shard.</p>
<p>See <a class="xref" href="docs-index_.html#index-wait-for-active-shards" title="Active shards">Active shards</a>.</p>
</dd>
<dt>
<span class="term">
<code class="literal">master_timeout</code>
</span>
</dt>
<dd>
(Optional, <a class="xref" href="common-options.html#time-units" title="Time units">time units</a>) Specifies the period of time to wait for
a connection to the master node. If no response is received before the timeout
expires, the request fails and returns an error. Defaults to <code class="literal">30s</code>.
</dd>
<dt>
<span class="term">
<code class="literal">timeout</code>
</span>
</dt>
<dd>
(Optional, <a class="xref" href="common-options.html#time-units" title="Time units">time units</a>) Specifies the period of time to wait for
a response. If no response is received before the timeout expires, the request
fails and returns an error. Defaults to <code class="literal">30s</code>.
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="indices-create-api-request-body"></a>Request body<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">aliases</code>
</span>
</dt>
<dd>
(Optional, <a class="xref" href="indices-aliases.html" title="Update index alias API">alias object</a>) Index aliases which include the
index. See <a class="xref" href="indices-aliases.html" title="Update index alias API">Update index alias</a>.
</dd>
<dt>
<span class="term">
<code class="literal">mappings</code>
</span>
</dt>
<dd>
<p>(Optional, <a class="xref" href="mapping.html" title="Mapping">mapping object</a>) Mapping for fields in the index. If
specified, this mapping can include:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
Field names
</li>
<li class="listitem">
<a class="xref" href="mapping-types.html" title="Field datatypes">Field datatypes</a>
</li>
<li class="listitem">
<a class="xref" href="mapping-params.html" title="Mapping parameters">Mapping parameters</a>
</li>
</ul>
</div>
<p>See <a class="xref" href="mapping.html" title="Mapping">Mapping</a>.</p>
</dd>
<dt>
<span class="term">
<code class="literal">settings</code>
</span>
</dt>
<dd>
(Optional, <a class="xref" href="index-modules.html#index-modules-settings" title="Index Settings">index setting object</a>) Configuration
options for the index. See <a class="xref" href="index-modules.html#index-modules-settings" title="Index Settings">Index Settings</a>.
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="indices-create-api-example"></a>Examples<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="create-index-settings"></a>Index settings<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h4>
</div></div></div>
<p>Each index created can have specific settings
associated with it, defined in the body:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, <a id="CO568-1"></a><i class="conum" data-value="1"></i>
            "number_of_replicas" : 2 <a id="CO568-2"></a><i class="conum" data-value="2"></i>
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1621.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO568-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Default for <code class="literal">number_of_shards</code> is 1</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO568-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Default for <code class="literal">number_of_replicas</code> is 1 (ie one replica for each primary shard)</p>
</td>
</tr>
</table>
</div>
<p>or more simplified</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1622.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>You do not have to explicitly specify <code class="literal">index</code> section inside the
<code class="literal">settings</code> section.</p>
</div>
</div>
<p>For more information regarding all the different index level settings
that can be set when creating an index, please check the
<a class="xref" href="index-modules.html" title="Index modules">index modules</a> section.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="mappings"></a>Mappings<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h4>
</div></div></div>
<p>The create index API allows for providing a mapping definition:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "properties" : {
            "field1" : { "type" : "text" }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1623.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>Before 7.0.0, the <em>mappings</em> definition used to include a type name. Although specifying
types in requests is now deprecated, a type can still be provided if the request parameter
include_type_name is set. For more details, please see <a class="xref" href="removal-of-types.html" title="Removal of mapping types"><em>Removal of mapping types</em></a>.</p>
</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="create-index-aliases"></a>Aliases<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h4>
</div></div></div>
<p>The create index API allows also to provide a set of <a class="xref" href="indices-aliases.html" title="Update index alias API">aliases</a>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test
{
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1624.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="create-index-wait-for-active-shards"></a>Wait For active shards<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/indices/create-index.asciidoc">edit</a>
</h4>
</div></div></div>
<p>By default, index creation will only return a response to the client when the primary copies of
each shard have been started, or the request times out. The index creation response will indicate
what happened:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "test"
}</pre>
</div>
<p><code class="literal">acknowledged</code> indicates whether the index was successfully created in the cluster, while
<code class="literal">shards_acknowledged</code> indicates whether the requisite number of shard copies were started for
each shard in the index before timing out. Note that it is still possible for either
<code class="literal">acknowledged</code> or <code class="literal">shards_acknowledged</code> to be <code class="literal">false</code>, but the index creation was successful.
These values simply indicate whether the operation completed before the timeout. If
<code class="literal">acknowledged</code> is <code class="literal">false</code>, then we timed out before the cluster state was updated with the
newly created index, but it probably will be created sometime soon. If <code class="literal">shards_acknowledged</code>
is <code class="literal">false</code>, then we timed out before the requisite number of shards were started (by default
just the primaries), even if the cluster state was successfully updated to reflect the newly
created index (i.e. <code class="literal">acknowledged=true</code>).</p>
<p>We can change the default of only waiting for the primary shards to start through the index
setting <code class="literal">index.write.wait_for_active_shards</code> (note that changing this setting will also affect
the <code class="literal">wait_for_active_shards</code> value on all subsequent write operations):</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test
{
    "settings": {
        "index.write.wait_for_active_shards": "2"
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1625.console"></div>
<p>or through the request parameter <code class="literal">wait_for_active_shards</code>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test?wait_for_active_shards=2</pre>
</div>
<div class="console_widget" data-snippet="snippets/1626.console"></div>
<p>A detailed explanation of <code class="literal">wait_for_active_shards</code> and its possible values can be found
<a class="xref" href="docs-index_.html#index-wait-for-active-shards" title="Active shards">here</a>.</p>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="indices-close.html">« Close index API</a>
</span>
<span class="next">
<a href="indices-delete-index.html">Delete index API »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>